﻿using System;
using System.Windows;
using System.Windows.Media;
using Creventive.Wpf.ShellFactory.Workspace;
using Creventive.Wpf.ShellFactory.Workspace.Primitives;

namespace MultiViewWithLogin.Infrastructure.Shell
{
    /// <summary>
    ///   Provides an extended view source with a title and an icon for use by the master views extension.
    /// </summary>
    public class WidgetViewSource : ApplicationViewSource
    {
        #region Static Fields/Constants

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(WidgetViewSource), new UIPropertyMetadata(null));
        public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(WidgetViewSource), new UIPropertyMetadata(null));
        public static readonly DependencyProperty SortOrderProperty = DependencyProperty.Register("SortOrder", typeof(int), typeof(WidgetViewSource), new UIPropertyMetadata(0));
        private static readonly DependencyPropertyKey IsLoadedPropertyKey = DependencyProperty.RegisterReadOnly("IsLoaded", typeof(bool), typeof(WidgetViewSource), new UIPropertyMetadata(false));
        public static readonly DependencyProperty IsLoadedProperty = IsLoadedPropertyKey.DependencyProperty;
        private static readonly DependencyPropertyKey ViewPropertyKey = DependencyProperty.RegisterReadOnly("View", typeof(ApplicationView), typeof(WidgetViewSource), new UIPropertyMetadata(null));
        public static readonly DependencyProperty ViewProperty = ViewPropertyKey.DependencyProperty;

        #endregion

        /// <summary>
        ///   Gets or sets an image to represent the application view.
        /// </summary>
        /// <value>The image.</value>
        public ImageSource Image
        {
            get { return (ImageSource)this.GetValue(ImageProperty); }
            set { this.SetValue(ImageProperty, value); }
        }

        public bool IsLoaded
        {
            get { return (bool)this.GetValue(IsLoadedProperty); }
            private set { this.SetValue(IsLoadedPropertyKey, value); }
        }

        /// <summary>
        ///   Gets or sets the sort order, representing where in the main menu this view source should be placed.
        /// </summary>
        /// <value>The sort order.</value>
        public int SortOrder
        {
            get { return (int)this.GetValue(SortOrderProperty); }
            set { this.SetValue(SortOrderProperty, value); }
        }

        /// <summary>
        ///   Gets or sets a title to represent the application view.
        /// </summary>
        /// <value>The title.</value>
        public string Title
        {
            get { return (string)this.GetValue(TitleProperty); }
            set { this.SetValue(TitleProperty, value); }
        }

        /// <summary>
        ///   Gets the view associated to this source (it it has been loaded).
        /// </summary>
        /// <value>The view.</value>
        public ApplicationView View
        {
            get { return (ApplicationView)this.GetValue(ViewProperty); }
            private set { this.SetValue(ViewPropertyKey, value); }
        }

        /// <summary>
        ///   Loads the application view referred to by this view source.
        /// </summary>
        /// <returns></returns>
        public override ApplicationView Load()
        {
            if (this.View == null)
            {
                this.View = base.Load();
                this.View.Closed += this.view_Closed;
                this.IsLoaded = true;
            }
            return this.View;
        }

        private void view_Closed(object sender, EventArgs e)
        {
            this.IsLoaded = false;
            this.View = null;
        }
    }
}